//
//  MyViewController.m
//  Exam16-2
//
//  Created by 孙晓晔 on 2017/5/31.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "MyViewController.h"

//#import <MobileCoreServices/MobileCoreServices.h>

//#import "UserProfileManager.h"
//#import "EditNicknameViewController.h"
//#import "UIImageView+HeadImage.h"

@interface MyViewController ()<UIImagePickerControllerDelegate,UIActionSheetDelegate>

@property (strong, nonatomic) UIImagePickerController *imagePicker;
@property (strong, nonatomic) UIImageView *headImageView;
@property (strong, nonatomic) UILabel *usernameLabel;

@property (strong, nonatomic) UIView *footerView;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.delegate = self;
    
    self.tableView.tableFooterView = self.footerView;
}

// 懒加载
- (UIView *)footerView {
    if (_footerView == nil) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
        _footerView.backgroundColor = [UIColor clearColor];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 0, _footerView.frame.size.width - 10, 0.5)];
        line.backgroundColor = [UIColor lightGrayColor];
        [_footerView addSubview:line];
        
        UIButton *logoutButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, _footerView.frame.size.width - 20, 45)];
        logoutButton.accessibilityIdentifier = @"logoff";
        
        UIColor *color = [UIColor colorWithRed:(0xfe)/255.0 green:(0x64)/255.0 blue:(0x50)/255.0 alpha:(1)];
        
        [logoutButton setBackgroundColor:color];
        
        NSString *username = [[EMClient sharedClient] currentUsername];
        NSString *logoutButtonTitle = [[NSString alloc] initWithFormat:@"退出(%@)", username];
        [logoutButton setTitle:logoutButtonTitle forState:UIControlStateNormal];
        [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [logoutButton addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:logoutButton];
    }
    
    return _footerView;
}

- (void)logoutAction {
    
}

// 懒加载
//- (UIImageView*)headImageView {
//    if (!_headImageView) {
//        _headImageView = [[UIImageView alloc] init];
//        _headImageView.frame = CGRectMake(20, 10, 60, 60);
//        _headImageView.contentMode = UIViewContentModeScaleToFill;
//    }
//    
//    UserProfileEntity *user = [[UserProfileManager sharedInstance] getCurUserProfile];
//    [_headImageView imageWithUsername:user.username placeholderImage:nil];
//    
//    return _headImageView;
//}

// 懒加载
//- (UILabel*)usernameLabel {
//    if (!_usernameLabel) {
//        _usernameLabel = [[UILabel alloc] init];
//        _usernameLabel.frame = CGRectMake(CGRectGetMaxX(_headImageView.frame) + 10.f, 10, 200, 20);
//        UserProfileEntity *user = [[UserProfileManager sharedInstance] getCurUserProfile];
//        _usernameLabel.text = user.username;
//        _usernameLabel.textColor = [UIColor lightGrayColor];
//    }
//    return _usernameLabel;
//}

// 懒加载
- (UIImagePickerController *)imagePicker {
    if (_imagePicker == nil) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.modalPresentationStyle= UIModalPresentationOverFullScreen;
        _imagePicker.allowsEditing = YES;
        _imagePicker.delegate = self;
    }
    
    return _imagePicker;
}


//#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        return 80;
    }
    
    return 50;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 15;
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

#pragma mark - UITableViewDataSource
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return @" ";
}

#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *strIndetifier = @"mycell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIndetifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strIndetifier];
    }
//    
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = @"头像";
        [cell.contentView addSubview:self.headImageView];
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"个人帐户";
        cell.detailTextLabel.text = self.usernameLabel.text;
    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"昵称";
//        UserProfileEntity *entity = [[UserProfileManager sharedInstance] getCurUserProfile];
//        if (entity && entity.nickname.length>0) {
//            cell.detailTextLabel.text = entity.nickname;
//        } else {
//            cell.detailTextLabel.text = [[EMClient sharedClient] currentUsername];
//        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照上传",@"相册上传", nil];
        [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
    } else if (indexPath.row == 1) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    } else if (indexPath.row == 2) {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"setting.editName", @"Edit NickName") delegate:self cancelButtonTitle:NSLocalizedString(@"cancel", @"Cancel") otherButtonTitles:NSLocalizedString(@"ok", @"OK"), nil];
//        [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
//        [alert show];
    }
}

@end
